package com.estronger.shopmanage.adapter.batchpg;

import android.app.Activity;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.estronger.shopmanage.R;
import com.estronger.shopmanage.bean.batch.BatchPgGoodsItem;
import com.estronger.shopmanage.bean.conditionitem;
import com.soonfor.evaluate.base.EvalBaseAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Soonfor on 2017/5/11.
 * 批量派工任务中产品的测量、安装条件
 */

public class BatchPgGoodsConditionAdapter extends EvalBaseAdapter<BatchPgGoodsConditionAdapter.ConditionHolder, BatchPgGoodsItem.GoodsCondition> {

    private int goodsIndex;//商品在列表的位置
    private ArrayList<BatchPgGoodsItem.GoodsCondition> conditionList;
    private String[] pos = {"①", "②", "③", "④", "⑤", "⑥", "⑦", "⑧", "⑨", "⑩","⑪","⑫","⑬","⑭","⑮","⑯","⑰","⑱","⑲","⑳"};
    private ConditonCheckedListener checkedListener;

    public BatchPgGoodsConditionAdapter(Activity context) {
        super(context);
    }

    @Override
    public void notifyDataSetChanged(List<BatchPgGoodsItem.GoodsCondition> dataList) {}
    public void notifyDataSetChanged(ArrayList<BatchPgGoodsItem.GoodsCondition> dataList, int goodsIndex) {
        this.conditionList = dataList;
        this.goodsIndex = goodsIndex;
        notifyDataSetChanged();
    }

    public void setCheckedListener(ConditonCheckedListener checkedListener) {
        this.checkedListener = checkedListener;
    }

    @Override
    public BatchPgGoodsConditionAdapter.ConditionHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.adapter_pg_batch_goodscodition, parent, false);
        ConditionHolder holder = new ConditionHolder(view);
        return holder;
    }
    @Override
    public void onBindViewHolder(@NonNull ConditionHolder holder, int position) {
        holder.setData(conditionList.get(position), position);
        holder.rlfItem.setTag(holder);
        holder.rlfItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    ConditionHolder vh = (ConditionHolder) v.getTag();
                    if(conditionList.get(vh.positon).getIfcheck().equals("1")){
                        conditionList.get(vh.positon).setIfcheck("0");
                        vh.ivfContionCheck.setImageResource(R.drawable.condition_nohave);
                        checkedListener.onCheckListener(conditionList, goodsIndex);
                    }else {
                        conditionList.get(vh.positon).setIfcheck("1");
                        vh.ivfContionCheck.setImageResource(R.drawable.condition_have);
                    }
                }catch (Exception e){
                    e.fillInStackTrace();
                }
            }
        });
    }
    
    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return conditionList == null ? 0 : conditionList.size();
    }

    public ArrayList<conditionitem> putCondition(List<BatchPgGoodsItem.GoodsCondition> yConditons){
        ArrayList<conditionitem> newConditons = new ArrayList<>();
        for(int i = 0; i<yConditons.size(); i++ ){
            BatchPgGoodsItem.GoodsCondition ycond = yConditons.get(i);
            conditionitem cond = new conditionitem();
            cond.setItemid(ycond.itemid);
            cond.setIfcheck(ycond.getIfcheck());
            newConditons.add(cond);
        }
        return newConditons;
    }

    public class ConditionHolder extends RecyclerView.ViewHolder {

        public int positon;
        RelativeLayout rlfItem;
        TextView tvfSerNumber, tvfCondtion;
        ImageView ivfContionCheck;
        public ConditionHolder(View view) {
            super(view);
            rlfItem = (RelativeLayout) view.findViewById(R.id.rlfConditionItem);
            tvfSerNumber = (TextView) view.findViewById(R.id.tvfSerialNumber);
            tvfCondtion = (TextView) view.findViewById(R.id.tvfCodtion);
            ivfContionCheck = (ImageView) view.findViewById(R.id.iv_conditon_check);
        }

        public void setData(final BatchPgGoodsItem.GoodsCondition condition, final int index) {
            this.positon = index;
            if (pos == null || pos.length == 0) {
                pos = new String[]{"①","②", "③", "④", "⑤", "⑥", "⑦", "⑧", "⑨", "⑩","⑪","⑫","⑬","⑭","⑮","⑯","⑰","⑱","⑲","⑳"};
            }
            try{
                tvfSerNumber.setText(pos[index]);
            }catch (Exception e){
               tvfSerNumber.setText("...");
            }
            tvfCondtion.setText(condition.getContent());
            if(condition.getIfcheck().equals("1")){
                ivfContionCheck.setImageResource(R.drawable.condition_have);
            }else{
                ivfContionCheck.setImageResource(R.drawable.condition_nohave);
            }
        }
    }

    //条件勾选变化监听
    public interface ConditonCheckedListener{
        void onCheckListener(ArrayList<BatchPgGoodsItem.GoodsCondition> conditionList, int goodsIndex);
    }
}
